<?php header('Content-type: text/html;charset=UTF-8')?>
<html>
	<head>
		<title>Сброс XDB</title>
	</head>
	<body>
		<h1>Скрипт сброса XDB в начальное состояние</h1><br/>
		<?php 
			if (!$_GET['accept']) {
				echo "<font color='red'>Вы уверены? <b>Данную операцию неельзя отменить!</b></font><br/>";
				echo "<a href='?accept=1' style='color: red'>Нажмите, чтобы сбросить БД.</a>&nbsp;";
			} else {
				nulldb();
				echo "<font color='green'>База успешно сброшена.</font><br/>";
				echo "Логин и пароль владельца сайта: <i>owner</i><br/>";
				echo "Логин и пароль администратора сайта: <i>admin</i><br/>";
			}
		?>
	</body>
</html>
<?php 
function nulldb() {
	// loading sql configuration
	require 'lib/Config.php';
	$dbc = new Config('sql');
	
	// connecting to database
	$GLOBALS['conn'] = mysqli_connect($dbc->host,$dbc->user, $dbc->password, $dbc->database);
	$conn = $GLOBALS['conn'];
	if (!$conn) die(mysqli_error($conn));
	
	// getting tables list
	$tlr = mysqli_query($conn, "SHOW TABLES");
	if (!$tlr) die(mysqli_error($conn));
	
	// making clear queryes...
	while ($nl = mysqli_fetch_array($tlr)) {
		$tbl = $nl[0];
		if ($tbl == '') continue;
		$query = "TRUNCATE TABLE `$tbl`";
		echo "clearing `$tbl`: $query... ";
		$res = mysqli_query($conn, $query);
		if (!$res) die('<font color="red">'.mysqli_error($conn).'</font>');
		echo "<font color='green'>OK</font><br/>";
	}
	
	// inserting Cercea object
	$cercea = insert_object('cercea', array('id'=>1));
	
	// inserting Administrators Group object
	$admins = insert_object('group', array('name'=>'Administrator'));
	// linking cercea with group
	insert_relation($cercea, $admins, 255);
	insert_relation($admins, $cercea, 255);
	
	// Inserting Users group object
	$users = insert_object('group', array('name'=>'User'));
	// linking cercea with group
	insert_relation($cercea, $users, 255);
	insert_relation($users, $cercea, 1);
	
	// Inserting Guests group object
	$guests = insert_object('group', array('name'=>'Guest'));
	// linking cercea with group
	insert_relation($cercea, $guests, 255);
	insert_relation($guests, $cercea, 1);
	
	// Inserting Banned group object
	$banned = insert_object('group', array('name'=>'Banned'));
	// linking cercea with group
	insert_relation($cercea, $banned, 255);
	
	// inserting owner user object
	$user = insert_object('user', array(
		'name'=>'Site owner',
		'reg'=>1,
		'approved'=>1
	));
	
	insert_relation($user, $cercea, 255);
	insert_relation($user, $admins, 255);
	insert_relation($user, $users, 255);
	insert_relation($user, $guests, 255);
	insert_relation($user, $banned, 255);
	
	// inserting Auth object 
	$auth = insert_object('auth', array(
		'login'=>md5('owner'),
		'password'=>md5('owner')
	));
	insert_relation($user, $auth, 255);
	
	// inserting administrator user
	$admin = insert_object('user', array(
		'name'=>'Site admin',
		'reg'=>1,
		'approved'=>1
	));
	
	$admin_skin = insert_object('skin', array(
		'name'=>'admin_interface',
		'author'=>'Dmitriy Chigevskiy',
		'weight'=>100
	));
	
	insert_relation($cercea, $admin, 1);
	insert_relation($admin, $cercea, 255);
	insert_relation($admin, $admins, 255);
	insert_relation($admins, $admin, 1);
	insert_relation($admins, $admin_skin, 1);
	insert_relation($admin, $users, 255);
	insert_relation($admin, $guests, 255);
	insert_relation($admin, $banned, 255);
	
	// inserting Auth object 
	$auth = insert_object('auth', array(
		'login'=>md5('admin'),
		'password'=>md5('admin')
	));
	insert_relation($admin, $auth, 255);

}
	function insert_object($type, $fields)
	{
		$conn = $GLOBALS['conn'];
		$keys = array_keys($fields);
		$q = "INSERT INTO `$type` (`".implode('`, `', $keys)."`) VALUES ('".implode("', '", $fields)."')";
		$res = mysqli_query($conn, $q);
		if (!$res)  die($q.' ==> '.mysqli_error($conn));
		$ret = array(
			'rt' => $type,
			'id' => mysqli_insert_id($conn)
		);
		
		return $ret;
	}
	
	function insert_relation($p, $c, $acl) {
		$conn = $GLOBALS['conn'];
		$res = mysqli_query($conn, "INSERT INTO `relations` ".
			"(`parent`, `parent_id`, `child`, `child_id`, `acl`)". 
			"VALUES ('{$p[rt]}', {$p[id]}, '{$c[rt]}', {$c[id]}, {$acl})");
		if (!$res)  die(mysqli_error($conn));
	}
?>